package com.xm.xmapp.map;

import android.graphics.Bitmap;
import android.graphics.Point;

import com.amap.api.maps.AMap;
import com.amap.api.maps.Projection;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.xm.xmapp.R;
import com.xm.xmapp.activity.MainActivity;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.tool.ImageTool;


import java.util.ArrayList;

public class MyGaodeImageView {

	private Marker marker;// marker类
	private ArrayList<ClusterItem> includeClusterItems;// 当前可观区域里的 聚合过之后的集合
	private LatLngBounds bounds;// 创建区域
	private AMap aMap;
	private int flag;
	private ClusterItem key;// 键，复用marker时标识
	private MarkerOptions options;

	public ClusterItem getKey() {
		return key;
	}

	public ArrayList<ClusterItem> getIncludeMarkers() {
		return includeClusterItems;
	}

	/**
	 * 头像加载完监听
	 * 
	 * @param aMap
	 */
	public MyGaodeImageView(ClusterItem firstMarkers, int gridSize, Projection projection, AMap aMap, int flag) {
		this.aMap = aMap;
		this.flag = flag;
		Point point = projection.toScreenLocation(firstMarkers.getLatLng());
		Point southwestPoint = new Point(point.x - gridSize, point.y + gridSize); // 范围类
		Point northeastPoint = new Point(point.x + gridSize, point.y - gridSize);// 范围类
		bounds = new LatLngBounds(projection.fromScreenLocation(southwestPoint),
				projection.fromScreenLocation(northeastPoint));
		// 设置初始化marker属性
		includeClusterItems = new ArrayList<ClusterItem>();
		includeClusterItems.add(firstMarkers);

	}

	public LatLngBounds getBounds() {
		return bounds;
	}

	/**
	 * 添加ClusterItem
	 */
	public void addClusterItem(ClusterItem clusterItem) {
		includeClusterItems.add(clusterItem);// 添加到列表中
	}

	public Marker getMarker() {
		return marker;
	}

	/**
	 * 
	 * @param marker
	 *            当传入的marker为空时，表示可以新建marker
	 */
	public void setMarker(Marker marker) {
		if (marker == null) {
			addNewMarker();
		} else {
			this.marker = marker;
		}
	}

	public void addNewMarker() {
		int size = includeClusterItems.size();
		if (size == 1) {
			this.marker = aMap.addMarker(options);
			this.marker.setObject(includeClusterItems.get(0));
		} else {
			this.marker = aMap.addMarker(options);
			this.marker.setObject(this);
		}
	}

	/**
	 * 设置聚合点的中心位置以及图标
	 */
	public void setKeyAndOptions() {
		int size = includeClusterItems.size();
		double lat = 0.0;
		double lng = 0.0;
		// 一个的时候
		if (size == 1) {// 设置marker单个属性
			// 设置marker位置
			key = includeClusterItems.get(0);
			LatLng latLng = includeClusterItems.get(0).getLatLng();
			options = new MarkerOptions();
			options.position(latLng).title(includeClusterItems.get(0).getNetWorkName());
			options.icon(BitmapDescriptorFactory.fromBitmap(getImageBitmap()));

		} else {// 聚合的时候
			// 设置marker聚合属性
			key = null;
			for (ClusterItem op : includeClusterItems) {
				lat += op.getLatLng().latitude;
				lng += op.getLatLng().longitude;
			}
			// 设置marker的位置为中心位置为聚集点的平均位置
			options = new MarkerOptions();
			options.position(new LatLng(lat / size, lng / size));
			options.icon(BitmapDescriptorFactory.fromBitmap(getImageBitmap()));

		}
	}

	public void reMoveMarker() {
		this.marker.remove();
	}

	/**
	 *            = 0为分时， =1位长租
	 * @return
	 */
	public Bitmap getImageBitmap() {
		ClusterItem clusterItem;
		if (includeClusterItems.size() == 1) {
			clusterItem = includeClusterItems.get(0);
//			return clusterItem.getImageBitmap(flag);
			return ImageTool.getBitmap1("网点", MyAppcation.getMyAppcation(),
					R.mipmap.home_color_piece);
		} else {
				return ImageTool.getBitmap1(includeClusterItems.size() + "聚合点", MyAppcation.getMyAppcation(),
						R.mipmap.home_color_piece);
		}
	}
}